using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using Microsoft.XLANGs.BaseTypes;
using System.Globalization;
using System.Resources;
using System.Threading;
namespace BTSProjectWizard.Forms
{
	public class SchemaPickerPage : BTSProjectWizard.BTSWizardPage
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button _findAssembly;
		private System.Windows.Forms.TextBox _assembly;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.OpenFileDialog _assemblyOpen;
		private System.Windows.Forms.ListBox _schemaList;
        private CheckBox _useXML;
		private System.ComponentModel.IContainer components = null;

		//ResourceManager _rm = new ResourceManager("BTSProjectWizard.Resources.Strings",Assembly.GetExecutingAssembly());
		public override bool ProcessSymbols()
		{
			bool valid = false;

            string schema = "System.Xml.XmlDocument";
            if(!_useXML.Checked)
                schema = _schemaList.SelectedItem!=null?_schemaList.SelectedItem.ToString():null;//_schemas[cr,0].ToString();
			if(schema==null||schema.Length==0)
			{
				MessageBox.Show(_rm.GetString("MissingSchmaMessage",Thread.CurrentThread.CurrentCulture));
				return valid;
				
			}
			wizard.AddSymbol(Data,schema);
            Reference r = new Reference();
            r.Path = _assembly.Text;
            if (!_useXML.Checked)
			    wizard.Info.References.Add(r);
			valid = true;
			return valid;
		}
		DataTable _schemasTable;
		public SchemaPickerPage()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call
			_schemasTable = new DataTable();
			_schemasTable.Locale=CultureInfo.InvariantCulture;
			_schemasTable.Columns.Add(_rm.GetString("SchemaTableType",Thread.CurrentThread.CurrentCulture));
			_schemasTable.Columns.Add(_rm.GetString("SchemaTableMessageType",Thread.CurrentThread.CurrentCulture));
			this.Title = _rm.GetString("SchemaPickerTitle",Thread.CurrentThread.CurrentCulture);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SchemaPickerPage));
            this.label1 = new System.Windows.Forms.Label();
            this._findAssembly = new System.Windows.Forms.Button();
            this._assembly = new System.Windows.Forms.TextBox();
            this._assemblyOpen = new System.Windows.Forms.OpenFileDialog();
            this.label2 = new System.Windows.Forms.Label();
            this._schemaList = new System.Windows.Forms.ListBox();
            this._useXML = new System.Windows.Forms.CheckBox();
            this.SuspendLayout();
            // 
            // label1
            // 
            resources.ApplyResources(this.label1, "label1");
            this.label1.Name = "label1";
            // 
            // _findAssembly
            // 
            resources.ApplyResources(this._findAssembly, "_findAssembly");
            this._findAssembly.Name = "_findAssembly";
            this._findAssembly.Click += new System.EventHandler(this._findAssembly_Click);
            // 
            // _assembly
            // 
            resources.ApplyResources(this._assembly, "_assembly");
            this._assembly.Name = "_assembly";
            this._assembly.TextChanged += new System.EventHandler(this._assembly_TextChanged);
            // 
            // _assemblyOpen
            // 
            resources.ApplyResources(this._assemblyOpen, "_assemblyOpen");
            // 
            // label2
            // 
            resources.ApplyResources(this.label2, "label2");
            this.label2.Name = "label2";
            // 
            // _schemaList
            // 
            resources.ApplyResources(this._schemaList, "_schemaList");
            this._schemaList.Name = "_schemaList";
            // 
            // _useXML
            // 
            resources.ApplyResources(this._useXML, "_useXML");
            this._useXML.Name = "_useXML";
            this._useXML.UseVisualStyleBackColor = true;
            this._useXML.CheckedChanged += new System.EventHandler(this._useXML_CheckedChanged);
            // 
            // SchemaPickerPage
            // 
            this.Controls.Add(this._useXML);
            this.Controls.Add(this._schemaList);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this._findAssembly);
            this.Controls.Add(this._assembly);
            this.Name = "SchemaPickerPage";
            resources.ApplyResources(this, "$this");
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void _findAssembly_Click(object sender, System.EventArgs e)
		{
			_assemblyOpen.ShowDialog();
			_assembly.Text = _assemblyOpen.FileName;
		}
		Assembly _asm;
		void LoadAssembly(string asmname)
		{
			_schemasTable.Clear();
			_schemaList.Items.Clear();
			_asm = Assembly.LoadFrom(asmname);
			object[] oa = _asm.GetCustomAttributes(typeof(BizTalkAssemblyAttribute),true);
			if(oa==null||oa.Length==0)
			{
				MessageBox.Show(_rm.GetString("NotABizTalkAssembly",Thread.CurrentThread.CurrentCulture));
				return;
			}

			
			Type[] types = _asm.GetTypes();
			SchemaRootsAttribute sra = null;
			SchemaTypeAttribute sta = null;
			for(int i=0;i<types.Length;i++)
			{
				if(types[i].BaseType ==typeof(SchemaBase)&&!types[i].IsNestedPublic)
				{ 
					//check if property schema
					oa = types[i].GetCustomAttributes(typeof(SchemaTypeAttribute),false);
					sta = (SchemaTypeAttribute)oa[0];
					if(sta.Type!=SchemaTypeEnum.Property)
					{
					//get the roots
					
					oa = types[i].GetCustomAttributes(typeof(SchemaRootsAttribute),false);
						if(oa!=null)
						{
							sra  = (SchemaRootsAttribute)oa[0];
							if(sra.Roots.Length>1)//multi-rooted schema!
							{
								for(int sr =0;sr<sra.Roots.Length;sr++)
								{
								
									AddSchema(types[i].GetNestedType(sra.Roots[sr]),types[i]);
								}
							}
							else
							{
								AddSchema(types[i],types[i]);
							}
						
							
						}

					}

				}
			}
			
		}
		void AddSchema(Type t,Type schemaType)
		{
			AddSchema(t,schemaType.FullName,((SchemaAttribute)t.GetCustomAttributes(typeof(SchemaAttribute),false)[0]));
						
		}
		void AddSchema(Type t,string tn,SchemaAttribute sa)
		{
			DataRow dr = _schemasTable.NewRow();
			string st = t.FullName.Replace("+",".");
			dr[0] = st;// tn;
			_schemaList.Items.Add(st);
			dr[1] = String.Format(CultureInfo.InvariantCulture,"{0}#{1}",sa.TargetNamespace,sa.RootElement);
			_schemaHash[dr[1].ToString()] = t;
			_schemasTable.Rows.Add(dr);
		}
		Hashtable _schemaHash = new Hashtable();
		private void _assembly_TextChanged(object sender, System.EventArgs e)
		{
			LoadAssembly(_assembly.Text);
		}

		private void _schemas_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
		{
			
		}

        private void _useXML_CheckedChanged(object sender, EventArgs e)
        {
            
        }
	}
	public class SingleSelectDataGrid : DataGrid 
 
	{ 
 
		private int oldSelectedRow = -1; 
 
      
 
		protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e) 
 
		{ 
 
			//don't call the base class if left mouse down 
 
			if(e.Button != MouseButtons.Left) 
 
				base.OnMouseMove(e); 
 
		} 
 

 
		protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e) 
 
		{ 
 
			//don't call the base class if in header 
 
			DataGrid.HitTestInfo hti = this.HitTest(new Point(e.X, e.Y)); 
 
			if(hti.Type == DataGrid.HitTestType.Cell) 
 
			{ 
 
				if(oldSelectedRow > -1) 
 
					this.UnSelect(oldSelectedRow); 
 
				oldSelectedRow = -1; 
 
				base.OnMouseDown(e); 
 
			} 
 
			else if(hti.Type == DataGrid.HitTestType.RowHeader) 
 
			{ 
 
				if(oldSelectedRow > -1) 
 
					this.UnSelect(oldSelectedRow); 
 
				if((Control.ModifierKeys & Keys.Shift) == 0) 
 
					base.OnMouseDown(e); 
 
				else 
 
					this.CurrentCell = new DataGridCell(hti.Row, hti.Column); 
 
				this.Select(hti.Row); 
 
				oldSelectedRow = hti.Row; 
 
			} 
 
		} 
 
	} 
 

}

